<?php

namespace App\Admin\Extensions;

use Encore\Admin\Form\Field;

class WangEditor extends Field
{

    const version = '2.1.23';

    protected $view = 'admin::form.editor';

    protected $pic_style = '';

    protected static $css = [
        '/packages/wangEditor-' . self::version . '/dist/css/wangEditor.min.css',
    ];

    protected static $js = [
        '/packages/wangEditor-' . self::version . '/dist/js/wangEditor.min.js',
    ];

    public function render()
    {
        $this->script = <<<EOT

var editor = new wangEditor('{$this->id}');
    
    editor.config.uploadImgUrl = '{$this->get_up_url()}';
    editor.config.uploadImgFileName = 'EditorImage'
    editor.config.uploadParams = {
        name:'EditorImage',
        pic_style:'{$this->pic_style}',
        _token: '{$this->get_token()}'
    };
    editor.create();

EOT;
        return parent::render();

    }

    public function setPicStyle($pic_style)
    {
        $this->pic_style = $pic_style;

        return $this;
    }

    private function get_token()
    {
        return csrf_token();
    }

    private function get_up_url()
    {
        return admin_url('upload-image');
    }
}